﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;

namespace EventScavenger
{
    public partial class PollingFilters : Form
    {
        public PollingFilters()
        {
            InitializeComponent();
        }

        private EventScavengerDAL eventScavengerDAL;

        public DialogResult Show(EventScavengerDAL eventScavengerDAL)
        {
            this.eventScavengerDAL = eventScavengerDAL;
            return ShowDialog();
        }

        #region Form events
        private void PollingFilters_Load(object sender, EventArgs e)
        {
            lvwPollingFilters.Items.Add("Loading...");
            backgroundWorkerLoading.RunWorkerAsync();
        }
        #endregion

        #region Loading
        private void backgroundWorkerLoading_DoWork(object sender, DoWorkEventArgs e)
        {
            System.Threading.Thread.Sleep(500);
            this.Invoke((MethodInvoker)delegate()
            {
                List<PollFilter> pollfilters = eventScavengerDAL.GetPollingFilterList();
                Application.DoEvents();
                lvwPollingFilters.Items.Clear();
                foreach (PollFilter pollfilter in pollfilters)
                {
                    ListViewItem lvi = new ListViewItem(pollfilter.Name);
                    lvi.SubItems.Add(pollfilter.ExcludeInfo ? "Exclude" : "Include");
                    lvi.SubItems.Add(pollfilter.ExcludeWarn ? "Exclude" : "Include");
                    lvi.SubItems.Add(pollfilter.ExcludeErr ? "Exclude" : "Include");
                    lvi.SubItems.Add(pollfilter.ExcludeSucAud ? "Exclude" : "Include");
                    lvi.SubItems.Add(pollfilter.ExcludeFailAud ? "Exclude" : "Include");
                    lvi.SubItems.Add((pollfilter.ExcludeEventIDs.Count() + pollfilter.IncludeEventIDs.Count()).ToString());
                    lvi.SubItems.Add((pollfilter.ExcludeSources.Count() + pollfilter.IncludeSources.Count()).ToString());
                    lvi.SubItems.Add(pollfilter.MessageContains);
                    lvi.Tag = pollfilter;
                    lvwPollingFilters.Items.Add(lvi);
                }
            });
        }
        #endregion

        #region Listview events
        private void lvwPollingFilters_Resize(object sender, EventArgs e)
        {
            int restWidth = 0;
            for (int i = 1; i < 9; i++)
            {
                restWidth += lvwPollingFilters.Columns[i].Width;
            }
            columnHeaderName.Width = lvwPollingFilters.ClientSize.Width - restWidth;
        }
        private void lvwPollingFilters_SelectedIndexChanged(object sender, EventArgs e)
        {
            toolStripButtonEdit.Enabled = lvwPollingFilters.SelectedItems.Count > 0;
            editToolStripMenuItem.Enabled = lvwPollingFilters.SelectedItems.Count > 0;
            toolStripButtonRemove.Enabled = (lvwPollingFilters.SelectedItems.Count > 0);
            deleteToolStripMenuItem.Enabled = (lvwPollingFilters.SelectedItems.Count > 0);
        }
        private void lvwPollingFilters_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                toolStripButtonEdit_Click(sender, e);
            }
        }
        #endregion

        #region Toolbar events
        private void toolStripButtonNew_Click(object sender, EventArgs e)
        {
            try
            {
                PollingFilterEdit pollingFilterEdit = new PollingFilterEdit();
                pollingFilterEdit.SelectedPollFilter = new PollFilter();
                if (pollingFilterEdit.Show(eventScavengerDAL) == DialogResult.OK)
                {

                    eventScavengerDAL.InsertPollFilter(pollingFilterEdit.SelectedPollFilter);
                    backgroundWorkerLoading.RunWorkerAsync();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "New", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void toolStripButtonEdit_Click(object sender, EventArgs e)
        {
            try
            {
                if (lvwPollingFilters.SelectedItems.Count == 1)
                {
                    PollingFilterEdit pollingFilterEdit = new PollingFilterEdit();
                    pollingFilterEdit.SelectedPollFilter = (PollFilter)lvwPollingFilters.SelectedItems[0].Tag;
                    if (pollingFilterEdit.Show(eventScavengerDAL) == DialogResult.OK)
                    {
                        eventScavengerDAL.UpdatePollFilter(pollingFilterEdit.SelectedPollFilter);
                        ListViewItem lvi = lvwPollingFilters.SelectedItems[0];
                        lvi.Text = pollingFilterEdit.SelectedPollFilter.Name;
                        lvi.SubItems[1].Text = (pollingFilterEdit.SelectedPollFilter.ExcludeInfo ? "Exclude" : "Include");
                        lvi.SubItems[2].Text = (pollingFilterEdit.SelectedPollFilter.ExcludeWarn ? "Exclude" : "Include");
                        lvi.SubItems[3].Text = (pollingFilterEdit.SelectedPollFilter.ExcludeErr ? "Exclude" : "Include");
                        lvi.SubItems[4].Text = (pollingFilterEdit.SelectedPollFilter.ExcludeSucAud ? "Exclude" : "Include");
                        lvi.SubItems[5].Text = (pollingFilterEdit.SelectedPollFilter.ExcludeFailAud ? "Exclude" : "Include");
                        lvi.SubItems[6].Text = ((pollingFilterEdit.SelectedPollFilter.ExcludeEventIDs.Count() + pollingFilterEdit.SelectedPollFilter.IncludeEventIDs.Count()).ToString());
                        lvi.SubItems[7].Text = ((pollingFilterEdit.SelectedPollFilter.ExcludeSources.Count() + pollingFilterEdit.SelectedPollFilter.IncludeSources.Count()).ToString());
                        lvi.SubItems[8].Text = (pollingFilterEdit.SelectedPollFilter.MessageContains);
                        lvwPollingFilters.SelectedItems[0].Tag = pollingFilterEdit.SelectedPollFilter;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Edit", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void toolStripButtonRemove_Click(object sender, EventArgs e)
        {
            if (lvwPollingFilters.SelectedItems.Count == 1)
            {
                if (MessageBox.Show("Are you sure you want to delete this polling filter?\r\nIf the polling filter is in use the delete will fail", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    try
                    {
                        eventScavengerDAL.DeletePollFilter((PollFilter)lvwPollingFilters.SelectedItems[0].Tag);
                        backgroundWorkerLoading.RunWorkerAsync();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Delete", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }
        #endregion

    }
}
